package com.tractorframework.core;

import java.lang.reflect.Field;

import android.util.Log;
import android.view.View;

public class ViewPort {
	
	public void instanceFieldView(Object target, View root)
	{
		Field[] fields = target.getClass().getDeclaredFields();
		if(fields == null)
		{
			return;
		}
		int length = fields.length;
		for(int i = 0; i < length; i++)
		{
			try {
				Field item = fields[i];
				Class clazz = item.getType();
				if(!View.class.isAssignableFrom(clazz))
				{
					continue;
				}
				boolean oldAccess = item.isAccessible();
				item.setAccessible(true);
				Object value = item.get(target);
				if(value == null)
				{
					int id = TractorUtil.getId(root.getContext(), item.getName());
					value = root.findViewById(id);
					item.set(target, value);
				}
				item.setAccessible(oldAccess);
			} catch (Exception ex)
			{
				Log.e(Constants.TAG, ex.getMessage());
			}
		}
	}
	
}
